package com.assets.Receive;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.Nullable;

import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;

import com.assets.Check.newTanWanCheck.BaseActivity1;
import com.assets.Ip_set.Url;
import com.assets.Listview_animation.BounceLoadingView;
import com.assets.R;
import com.assets.bean.Department_bean;
import com.assets.newNHF.util.LogUtils;
import com.assets.util.MyOkHttp;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Response;

/**
 * Created by Administrator on 2019/3/11.
 */

public class Receive_property extends BaseActivity1 {
    private SearchView sv;
    private ListView lv;
    private List<String> X = new ArrayList<>();
    private List<String> p = new ArrayList<String>();
    private String url;
    private BounceLoadingView loadingView;
    private Object[] names;
    private ArrayList<String> stringList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_category);
        url = "http://" + Url.IP + "/api/namesQuery";
        lv = (ListView) findViewById(R.id.lv_allot_sl);
        lv.setTextFilterEnabled(true);//设置lv可以被过虑
        sv = (SearchView) findViewById(R.id.tv_allot_sl);
        loadingView = (BounceLoadingView) findViewById(R.id.loadingView);
        loadingView.addBitmap(R.mipmap.v4);
        loadingView.addBitmap(R.mipmap.v5);
        loadingView.addBitmap(R.mipmap.v6);
        loadingView.addBitmap(R.mipmap.v7);
        loadingView.addBitmap(R.mipmap.v8);
        loadingView.addBitmap(R.mipmap.v9);
        loadingView.setShadowColor(Color.LTGRAY);
        loadingView.setDuration(700);
        loadingView.start();
        load();
        // 设置该SearchView默认是否自动缩小为图标
        sv.setIconifiedByDefault(false);
        sv.setQueryHint("查找");
        // 为该SearchView组件设置事件监听器
        // 设置该SearchView显示搜索按钮
        sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                // 实际应用中应该在该方法内执行实际查询
                // 此处仅使用Toast显示用户输入的查询内容
                showToast("您的选择是:" + query);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                Object[] obj = searchItem(newText);
                updateLayout(obj);
                return false;

            }
        });
        lv.setOnItemClickListener((adapterView, view, i, l) -> {
            Intent intent = new Intent();
            String name = (String) adapterView.getItemAtPosition(i);//获取当前点击的item的字符串内容
            int position = X.indexOf(name);
            if (position != -1) {
                String id = p.get(position);
                intent.putExtra("name", name);
                intent.putExtra("num", id);
            }
            Receive_property.this.setResult(4, intent);
            Receive_property.this.finish();//把item的内容返回给之前的activity
        });
    }

    private void load() {
        stringList = getIntent().getStringArrayListExtra("stringList");
        String company = getIntent().getStringExtra("company_id");
        Map<String, String> map = new HashMap<>();
        if (!TextUtils.isEmpty(company)) {
            map.put("companyId", company);
            LogUtils.d(url+"companyId"+company);
        }
        MyOkHttp.getInstance().post(url, map, new MyOkHttp.requestMessage() {
            @Override
            public void requestFail() {
                LogUtils.d("连接服务器失败");
            }

            @Override
            public void requestComplete() {

            }

            @Override
            public void requestSuccess(Response response) throws IOException {
                String responseData = response.body().string();
                showResponse(responseData);
            }
        });

    }

    private void showResponse(final String responseData) {
        LogUtils.d(responseData);
        Gson gson = new Gson();
        List<Department_bean> userList = gson.fromJson(responseData, new TypeToken<List<Department_bean>>() {
        }.getType());
        for (int i = 0; i < userList.size(); i++) {
            if (stringList.contains(userList.get(i).getName())) {
                continue;
            }
            X.add(userList.get(i).getName());
            p.add(userList.get(i).getNum());
        }
        if (X.size() == 0) {
            showToast("可选资产数量为0");
            finish();
            return;
        }
        names = X.toArray();
        runOnUiThread(() -> {
            loadingView.setVisibility(View.GONE);
            lv.setAdapter(new ArrayAdapter<Object>(getApplicationContext(),
                    R.layout.listview_style, names));
        });
    }

    public void updateLayout(Object[] obj) {
        lv.setAdapter(new ArrayAdapter<Object>(getApplicationContext(),
                R.layout.listview_style, obj));
    }

    public Object[] searchItem(String name) {
        ArrayList<String> mSearchList = new ArrayList<String>();
        for (int i = 0; i < X.size(); i++) {
            int index = X.get(i).indexOf(name);
            // 存在匹配的数据
            if (index != -1) {
                mSearchList.add(X.get(i));
            }
        }
        return mSearchList.toArray();
    }
}
